package com.andnnl.stack1;

import java.util.Stack;

public class ArithmeticExpressionParser {
    public static int evaluateExpression(String expression) {
        char[] tokens = expression.toCharArray();

        // 创建两个栈，一个用于操作数，一个用于操作符
        Stack<Integer> operandStack = new Stack<>();
        Stack<Character> operatorStack = new Stack<>();

        for (int i = 0; i < tokens.length; i++) {
            // 忽略空格
            if (tokens[i] == ' ') {
                continue;
            }

            // 处理数字
            if (Character.isDigit(tokens[i])) {
                StringBuffer sb = new StringBuffer();
                // 将多位数拼接起来
                while (i < tokens.length && Character.isDigit(tokens[i])) {
                    sb.append(tokens[i++]);
                }
                i--;
                // 将操作数入栈
                operandStack.push(Integer.parseInt(sb.toString()));
            }
            // 处理括号
            else if (tokens[i] == '(') {
                operatorStack.push(tokens[i]);
            } else if (tokens[i] == ')') {
                while (!operatorStack.isEmpty() && operatorStack.peek() != '(') {
                    processOperator(operandStack, operatorStack);
                }
                operatorStack.pop(); // 弹出 '('
            }
            // 处理操作符
            else if (isOperator(tokens[i])) {
                while (!operatorStack.isEmpty() && operatorPrecedence(operatorStack.peek()) >= operatorPrecedence(tokens[i])) {
                    processOperator(operandStack, operatorStack);
                }
                operatorStack.push(tokens[i]);
            }
        }

        // 处理剩余的操作符
        while (!operatorStack.isEmpty()) {
            processOperator(operandStack, operatorStack);
        }

        // 返回最终结果
        return operandStack.pop();
    }

    // 判断是否为操作符
    private static boolean isOperator(char ch) {
        return ch == '+' || ch == '-' || ch == '*' || ch == '/';
    }

    // 获取操作符的优先级
    private static int operatorPrecedence(char operator) {
        switch (operator) {
            case '+':
            case '-':
                return 1;
            case '*':
            case '/':
                return 2;
            default:
                return 0;
        }
    }

    // 处理操作符，计算结果并入栈
    private static void processOperator(Stack<Integer> operandStack, Stack<Character> operatorStack) {
        char operator = operatorStack.pop();
        int operand2 = operandStack.pop();
        int operand1 = operandStack.pop();
        int result = performOperation(operator, operand1, operand2);
        operandStack.push(result);
    }

    // 执行操作
    private static int performOperation(char operator, int operand1, int operand2) {
        switch (operator) {
            case '+':
                return operand1 + operand2;
            case '-':
                return operand1 - operand2;
            case '*':
                return operand1 * operand2;
            case '/':
                if (operand2 == 0) {
                    throw new ArithmeticException("除数不能为零");
                }
                return operand1 / operand2;
            default:
                return 0;
        }
    }

    public static void main(String[] args) {
        String expression = "3 + 4 * (4 - 2) / 2"; // 表达式示例
        int result = evaluateExpression(expression);
        System.out.println("结果: " + result);
    }
}